package com.googlecode.moneyManager.service;

import java.util.Collections;
import java.util.List;

import com.googlecode.moneyManager.dao.CustomerDAO;
import com.googlecode.moneyManager.dao.DepositDAO;
import com.googlecode.moneyManager.dao.GenericDAO;
import com.googlecode.moneyManager.model.Amount;
import com.googlecode.moneyManager.model.Deposit;
import com.googlecode.moneyManager.model.Replenish;
import com.googlecode.moneyManager.model.Customer;

/**
 * @author Sergiy Doroshenko Jul 5, 2008 11:16:27 AM
 */
public class DepositeServiceImpl implements DepositeService {

    DepositDAO depositDAO;
    
    CustomerDAO customerDAO;
    
    GenericDAO<Replenish> replenishDAO;

    GenericDAO<Amount> amountDAO;

    public List<Deposit> getListOfDeposits() {
        return depositDAO.getAll();
    }
    
    public boolean delete(Deposit d) {

        for (Replenish replenish : d.getReplenish()) {
            amountDAO.delete(replenish.getAmount());
            replenishDAO.delete(replenish);
        }
        depositDAO.delete(d);

        return true;
    }

    public GenericDAO<Amount> getAmountDAO() {
        return amountDAO;
    }

    public void setAmountDAO(GenericDAO<Amount> amountDAO) {
        this.amountDAO = amountDAO;
    }


    public void setDepositDAO(DepositDAO depositDAO) {
        this.depositDAO = depositDAO;
    }


    public void setReplenishDAO(GenericDAO<Replenish> replenishDAO) {
        this.replenishDAO = replenishDAO;
    }

    public List<Deposit> getDepositsByUser(String fname, String sname) {
	List<Customer> users = customerDAO.getUsersByName(fname, sname);
	if(users.size() < 1) return Collections.EMPTY_LIST;
	return depositDAO.getDepositsByUser(users.get(0));
    }


    public void setUserDAO(CustomerDAO customerDAO) {
        this.customerDAO = customerDAO;
    }

}
